from flask_sqlalchemy import SQLAlchemy
from apps.pojo.Entity import User

db = SQLAlchemy()

class UserRepository:
    @staticmethod
    def create_user(username, email):
        user = User(username=username, email=email)
        db.session.add(user)
        db.session.commit()
        return user

    @staticmethod
    def get_user_by_id(user_id):
        return User.query.get(user_id)

    @staticmethod
    def get_all_users():
        users = User.query.all()
        dict = {}
        for i in users:
            dict[repr(i.id)] = i.as_dict()
        return dict

    @staticmethod
    def get_all_users_count():
        return User.query.count()

    @staticmethod
    def update_user(user, new_username):
        user.username = new_username
        db.session.commit()

    @staticmethod
    def delete_user(user):
        db.session.delete(user)
        db.session.commit()